package com.ss.service.support;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ss.core.exceptions.SummerExecuteException;
import com.ss.entity.*;
import com.ss.mapper.*;
import com.ss.utlis.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;

/**
 * 审核操作节点相关方法
 * @Author: 夏十七
 * @Date: 2019/7/28 17:38
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ExecuteMember {

    @Autowired
    @SuppressWarnings("all")
    private SummerNodeMemberMapper summerNodeMemberMapper;
    @Autowired
    @SuppressWarnings("all")
    private SummerAppointMemberMapper summerAppointMemberMapper;
    @Autowired
    @SuppressWarnings("all")
    private SummerAppointMemberGlobalMapper summerAppointMemberGlobalMapper;
    @Autowired
    @SuppressWarnings("all")
    private SummerGroupMapper summerGroupMapper;
    @Autowired
    @SuppressWarnings("all")
    private SummerGroupMemberMapper summerGroupMemberMapper;

    /**
     * 验证权限
     * @param summerInstance 实例id 节点id
     * @param memberId 审核人id
     * @return
     */
    public SummerNodeMember provingAuth(SummerInstance summerInstance,String memberId){
        if (summerInstance.getNode() == null){
            throw new SummerExecuteException("验证审核权限时，当前所在节点为空");
        }
        //查询是否存在于节点审核人列表
        QueryWrapper<SummerNodeMember> query = Wrappers.query();
        query.eq("node_id",summerInstance.getNode().getId());
        query.eq("member_id",memberId);
        SummerNodeMember summerNodeMember = summerNodeMemberMapper.selectOne(query);
        if (summerNodeMember == null){
            throw new SummerExecuteException("审核异常，无该审核权限");
        }
        if (!summerNodeMember.getIsUse()){
            throw new SummerExecuteException("审核异常，审核权限已被停用");
        }
        //查询是否指定节点
        QueryWrapper<SummerAppointMember> query1 = Wrappers.query();
        query1.eq("instance_id",summerInstance.getId());
        query1.eq("node_id",summerInstance.getNode().getId());
        List<SummerAppointMember> summerAppointMembers = summerAppointMemberMapper.selectList(query1);
        //不等于null 又不存在与指定人列表中则没有审核权限
        if (summerAppointMembers != null && summerAppointMembers.size()!=0){
            if (! ListUtil.contains(summerAppointMembers, e -> e.getMemberId().equals(memberId))){
                throw new SummerExecuteException("审核异常，无该审核权限");
            }
        }
        QueryWrapper<SummerAppointMemberGlobal> query2 = Wrappers.query();
        query2.eq("instance_id",summerInstance.getId());
        List<SummerAppointMemberGlobal> summerAppointGlobalMembers = summerAppointMemberGlobalMapper.selectList(query2);
        //不等于null 又不存在与指定人列表中则没有审核权限
        if (summerAppointGlobalMembers != null && summerAppointGlobalMembers.size()!=0){
            if (!ListUtil.contains(summerAppointGlobalMembers, e -> e.getMemberId().equals(memberId))){
                throw new SummerExecuteException("审核异常，无该审核权限");
            }
        }
        return summerNodeMember;
    }


    /**
     * 该节点是否包含这些审核人，不包含则抛出异常
     * @param nodeId 节点id
     * @param members
     */
    public void contain(Integer nodeId,String[] members){
        //查询是否存在于节点审核人列表
        QueryWrapper<SummerNodeMember> query = Wrappers.query();
        query.eq("node_id",nodeId);
        query.in("member_id", Arrays.asList(members));
        List<SummerNodeMember> summerNodeMembers = summerNodeMemberMapper.selectList(query);
        //如果查出来的数量不同，那么检查是谁错了
        if (summerNodeMembers.size() != members.length){
            for (String member : members) {
                if (!ListUtil.contains(summerNodeMembers, e->member.equals(e.getMemberId()))){
                    throw new SummerExecuteException("审核人：" + member+"不存在于该节点的审核人列表中");
                }
            }
        }
    }

    /**
     * 该审核流是否包含这些审核人，不包含则抛出异常
     * @param flowId 节点id
     * @param members
     */
    public void containByFlow(Integer flowId, String[] members) {
        //查询是否存在于节点审核人列表
        List<SummerNodeMember> summerNodeMembers = this.getNodeMemberByFlowId(flowId);
        //如果查出来的数量不同，那么检查是谁错了
        for (String member : members) {
            if (!ListUtil.contains(summerNodeMembers, e->member.equals(e.getMemberId()))){
                throw new SummerExecuteException("审核人：" + member+"不存在于审核人列表中");
            }
        }
    }

    /**
     * 获取组下的所有人，加上子组的
     * @param sourceId  源Id
     * @return
     */
    public List<SummerGroupMember> getMemberBySourceId(String sourceId){
        //查询组以及他的子组
        SummerGroup group = summerGroupMapper.selectGroupAndSonBySourceId(sourceId);
        if (group == null){
            throw  new SummerExecuteException("组不存在");
        }
        List<SummerGroup> summerGroups = group.getAllGroupList();
        //查询成员
       return summerGroupMemberMapper.selectByGroupIdIn(summerGroups);
    }

    /**
     * 查询一个审批流的所有审核人
     * @param flowId 流Id
     * @return
     */
    public List<SummerNodeMember> getNodeMemberByFlowId(Integer flowId){
        return summerNodeMemberMapper.selectByFlowId(flowId);
    }

    /**
     * 查询一个节点的审核人
     * @param nodeId 节点Id
     * @return
     */
    public List<SummerNodeMember> getNodeMemberByNodeId(Integer nodeId){
        QueryWrapper<SummerNodeMember> query = Wrappers.query();
        query.eq("node_id",nodeId);
        return summerNodeMemberMapper.selectList(query);
    }



}
